<?php
//+---------------------------------------------------------------------------------------------------------------------
//| 人生是荒芜的旅行，冷暖自知，苦乐在心
//+---------------------------------------------------------------------------------------------------------------------
//| Author:Janmas <janmas@126.com>
//+---------------------------------------------------------------------------------------------------------------------
//| 
//+---------------------------------------------------------------------------------------------------------------------


namespace helper\upload;


use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\File;

class QiniuOss extends BaseUpload
{

	/**
	 * @inheritDoc
	 */
	public function upload( File $file = null,$justGetFileName=false )
	{
        $this->validate($file);
        $uploadManager = new UploadManager();
        $auth = new Auth($this->config['access-key'],$this->config['secret-key']);
        $token = $auth->uploadToken($this->config['bucket']);
        $filename = date('Ymd') . '/' . $file->getOriginalName();
        list($ret, $error) = $uploadManager->putFile($token, $filename, $file->getPathname());

        if(!is_null($error)){
            throw new \Exception('七牛云上传错误，错误码：'.$error['code'].'，错误描述：'.$error['error']);
        }

        if($justGetFileName){
            $this->fileInfo['path'] = $filename;
        }else{
            $this->fileInfo['path'] = $this->config['domain'] . $filename;
        }

        $this->fileInfo['thumb'] = $this->isVideo($file)?$this->fileInfo['path'] . '?vframe/jpg/offset/1':'';
        return $this->fileInfo;

	}
}